﻿@using Entidades.Sistema.Base
@using System.Collections.ObjectModel;

@{
    Html.EnableClientValidation(true);
    Html.EnableUnobtrusiveJavaScript(true);

    var grid = Html.DevExpress().GridView(
        settings =>
        {
            settings.Name = "gvEditing";
            settings.KeyFieldName = "IdCliente";
            settings.CallbackRouteValues = new { Controller = "Cliente", Action = "EditClientesPartial" };

            settings.Columns.Add(column =>
            {
                column.Caption = "#";
                column.SetDataItemTemplateContent(c =>
                {
                    if (ViewBag.PermisoEdit)
                    {
                        ViewContext.Writer.Write(

                        Html.ActionLink("Editar", "EditClientesUpdatePartial", new { IdCliente = DataBinder.Eval(c.DataItem, "IdCliente") }) + "&nbsp;"
                        );
                    }
                    if (ViewBag.PermisoDelete)
                    {
                        ViewContext.Writer.Write(

                        Html.ActionLink("Eliminar", "EditClientesDeletePartial", new { IdCliente = DataBinder.Eval(c.DataItem, "IdCliente") },
                            new { onclick = "return confirm('Realmente quiere elimnar el Cliente?')" }) );
                    }
                    
                });
                column.SetHeaderTemplateContent(c =>
                {
                    if (ViewBag.PermisoCreate)
                    {
                        ViewContext.Writer.Write(
                        Html.ActionLink("Nuevo", "EditClientesUpdatePartial", new { IdCliente = -1 }).ToHtmlString());                    
                    }                    
                });
                column.Settings.AllowDragDrop = DefaultBoolean.False;
                column.Settings.AllowSort = DefaultBoolean.False;
                column.Width = 70;
            });            
            
            
            settings.SettingsBehavior.ConfirmDelete = true;
            settings.SettingsPopup.EditForm.Width = 600;
            settings.SettingsPopup.EditForm.HorizontalAlign = PopupHorizontalAlign.Center;
            settings.SettingsPopup.EditForm.VerticalAlign = PopupVerticalAlign.WindowCenter;
            settings.SettingsPager.FirstPageButton.Visible = true;
            settings.SettingsPager.LastPageButton.Visible = true;
            settings.SettingsPager.PageSizeItemSettings.Visible = true;
            settings.SettingsPager.PageSizeItemSettings.Items = new string[] { "10", "20", "50" };
            
            
            settings.Settings.ShowFilterRow = true;
            settings.Settings.ShowFilterRowMenu = true;
            settings.CommandColumn.Visible = true;
            settings.CommandColumn.ShowClearFilterButton = true;
            
            settings.Width = System.Web.UI.WebControls.Unit.Percentage(100);

            settings.Columns.Add("Nombre");



            settings.Columns.Add(column =>
            {
                column.FieldName = "TipoCliente.IdTipoCliente";
                column.Caption = "Tipo Cliente";

                column.ColumnType = MVCxGridViewColumnType.ComboBox;
                var comboBoxProperties = column.PropertiesEdit as ComboBoxProperties;
                comboBoxProperties.DataSource = ViewBag.TipoClientes;
                comboBoxProperties.TextField = "Nombre";
                comboBoxProperties.ValueField = "IdTipoCliente";
                comboBoxProperties.ValidationSettings.ErrorDisplayMode = ErrorDisplayMode.Text;
                comboBoxProperties.IncrementalFilteringMode = IncrementalFilteringMode.StartsWith;
                comboBoxProperties.DropDownStyle = DropDownStyle.DropDown;

                column.SetEditItemTemplateContent(container =>
                {
                    Html.DevExpress().ComboBox(editorSettings =>
                    {
                        editorSettings.Name = "IdTipoCliente";
                        editorSettings.Properties.TextField = "Nombre";
                        editorSettings.Properties.ValueField = "IdTipoCliente";
                        editorSettings.Properties.Width = 120;
                        editorSettings.Properties.ValueType = typeof(int);
                        editorSettings.Properties.ValidationSettings.EnableCustomValidation = true;
                        editorSettings.Properties.ValidationSettings.ErrorDisplayMode = ErrorDisplayMode.ImageWithText;
                        editorSettings.ShowModelErrors = true;
                        editorSettings.Properties.ValidationSettings.ErrorDisplayMode = ErrorDisplayMode.ImageWithTooltip;
                        editorSettings.Properties.IncrementalFilteringMode = IncrementalFilteringMode.StartsWith;

                    }).BindList(ViewBag.TipoClientes).Bind(DataBinder.Eval(container.DataItem, "TipoCliente.IdTipoCliente")).Render();
                });
            });




            settings.Columns.Add(column =>
            {
                column.FieldName = "CondicionIva.IdCondicionIva";
                column.Caption = "Condicion Iva";

                column.ColumnType = MVCxGridViewColumnType.ComboBox;
                var comboBoxProperties = column.PropertiesEdit as ComboBoxProperties;
                comboBoxProperties.DataSource = ViewBag.CondicionIva;
                comboBoxProperties.TextField = "Nombre";
                comboBoxProperties.ValueField = "IdCondicionIva";
                comboBoxProperties.ValidationSettings.ErrorDisplayMode = ErrorDisplayMode.Text;
                comboBoxProperties.IncrementalFilteringMode = IncrementalFilteringMode.StartsWith;
                comboBoxProperties.DropDownStyle = DropDownStyle.DropDown;

                column.SetEditItemTemplateContent(container =>
                {
                    Html.DevExpress().ComboBox(editorSettings =>
                    {
                        editorSettings.Name = "IdCondicionIva";
                        editorSettings.Properties.TextField = "Nombre";
                        editorSettings.Properties.ValueField = "IdCondicionIva";
                        editorSettings.Properties.Width = 120;
                        editorSettings.Properties.ValueType = typeof(int);
                        editorSettings.Properties.ValidationSettings.EnableCustomValidation = true;
                        editorSettings.Properties.ValidationSettings.ErrorDisplayMode = ErrorDisplayMode.ImageWithText;
                        editorSettings.ShowModelErrors = true;
                        editorSettings.Properties.ValidationSettings.ErrorDisplayMode = ErrorDisplayMode.ImageWithTooltip;
                        editorSettings.Properties.IncrementalFilteringMode = IncrementalFilteringMode.StartsWith;

                    }).BindList(ViewBag.CondicionIva).Bind(DataBinder.Eval(container.DataItem, "CondicionIva.IdCondicionIva")).Render();
                });
            });
            
            

            //settings.Columns.Add(
            //    column =>
            //    {
            //        column.FieldName = "CondicionIva.IdCondicionIva";
            //        column.Caption = "Condicion Iva";

            //        column.ColumnType = MVCxGridViewColumnType.ComboBox;
            //        var comboBoxProperties = column.PropertiesEdit as ComboBoxProperties;
            //        comboBoxProperties.DataSource = ViewBag.CondicionIva;
            //        comboBoxProperties.TextField = "Nombre";
            //        comboBoxProperties.ValueField = "IdCondicionIva";
            //        comboBoxProperties.ValueType = typeof(int);
            //        comboBoxProperties.DropDownStyle = DropDownStyle.DropDown;
            //    });

            settings.Columns.Add(columnProvincia =>
            {
                columnProvincia.Caption = "Provincia";
                columnProvincia.FieldName = "IdProvincia";

                columnProvincia.ColumnType = MVCxGridViewColumnType.ComboBox;
                ComboBoxProperties propertiesComboBox = columnProvincia.PropertiesEdit as ComboBoxProperties;
                propertiesComboBox.DataSource = ViewBag.Provincia;
                propertiesComboBox.TextField = "Nombre";
                propertiesComboBox.ValueField = "IdProvincia";
                propertiesComboBox.ValueType = typeof(int);

                propertiesComboBox.EnableSynchronization = DefaultBoolean.False;
                propertiesComboBox.IncrementalFilteringMode = IncrementalFilteringMode.StartsWith;
                
            });

            settings.Columns.Add(columnLocalidad =>
            {
                columnLocalidad.Caption = "Localidad";
                columnLocalidad.FieldName = "IdLocalidad";

                columnLocalidad.ColumnType = MVCxGridViewColumnType.ComboBox;
                ComboBoxProperties propertiesComboBox = columnLocalidad.PropertiesEdit as ComboBoxProperties;
                propertiesComboBox.DataSource = ViewBag.Localidad;
                propertiesComboBox.TextField = "Nombre";
                propertiesComboBox.ValueField = "IdLocalidad";
                propertiesComboBox.ValueType = typeof(int);

                propertiesComboBox.EnableSynchronization = DefaultBoolean.False;
                propertiesComboBox.IncrementalFilteringMode = IncrementalFilteringMode.StartsWith;

            });



            settings.Columns.Add(column =>
            {
                column.FieldName = "Barrio.IdBarrio";
                column.Caption = "Barrio";

                column.ColumnType = MVCxGridViewColumnType.ComboBox;
                var comboBoxProperties = column.PropertiesEdit as ComboBoxProperties;
                comboBoxProperties.DataSource = ViewBag.Barrio;
                comboBoxProperties.TextField = "Nombre";
                comboBoxProperties.ValueField = "IdBarrio";
                comboBoxProperties.ValidationSettings.ErrorDisplayMode = ErrorDisplayMode.Text;
                comboBoxProperties.IncrementalFilteringMode = IncrementalFilteringMode.StartsWith;
                comboBoxProperties.DropDownStyle = DropDownStyle.DropDown;

                column.SetEditItemTemplateContent(container =>
                {
                    Html.DevExpress().ComboBox(editorSettings =>
                    {
                        editorSettings.Name = "IdBarrio";
                        editorSettings.Properties.TextField = "Nombre";
                        editorSettings.Properties.ValueField = "IdBarrio";
                        editorSettings.Properties.Width = 120;
                        editorSettings.Properties.ValueType = typeof(int);
                        editorSettings.Properties.ValidationSettings.EnableCustomValidation = true;
                        editorSettings.ShowModelErrors = true;
                        editorSettings.Properties.ValidationSettings.ErrorDisplayMode = ErrorDisplayMode.ImageWithTooltip;
                        editorSettings.Properties.IncrementalFilteringMode = IncrementalFilteringMode.StartsWith;

                    }).BindList(ViewBag.Barrio).Bind(DataBinder.Eval(container.DataItem, "Barrio.IdBarrio")).Render();
                });
            });
            

            //settings.Columns.Add(columnBarrio =>
            //{
            //    columnBarrio.Caption = "Barrio";
            //    columnBarrio.FieldName = "Barrio.IdBarrio";

            //    columnBarrio.ColumnType = MVCxGridViewColumnType.ComboBox;
            //    ComboBoxProperties propertiesComboBox = columnBarrio.PropertiesEdit as ComboBoxProperties;
            //    propertiesComboBox.DataSource = ViewBag.Barrio;
            //    propertiesComboBox.TextField = "Nombre";
            //    propertiesComboBox.ValueField = "IdBarrio";
            //    propertiesComboBox.ValueType = typeof(int);

            //    propertiesComboBox.EnableSynchronization = DefaultBoolean.False;
            //    propertiesComboBox.IncrementalFilteringMode = IncrementalFilteringMode.StartsWith;

            //});

            settings.PreRender = (s, e) =>
            {
                var sender = (MVCxGridView)s;
                sender.FocusedRowIndex = 1;
            };

            settings.SettingsBehavior.AllowFocusedRow = true;
        });

    if (ViewData["EditError"] != null){
        grid.SetEditErrorText((string)ViewData["EditError"]);
    }
    @Html.ValidationMessage("CustomError")
}

@grid.Bind(Model).GetHtml()




